#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
主题: 基本的日期与时间转换
问题: 你需要执行简单的时间转换，比如天到秒，小时到分钟等的转换。
提示 : 
"""

from datetime import timedelta, datetime
from dateutil.relativedelta import relativedelta

def recipe1():
    """执行不同时间单位的转换和计算"""
    a = timedelta(days=2, hours=6)
    b = timedelta(hours=4.5)
    print(f"{a = }")
    print(f"{b = }")

    c = a + b
    print(f"{c = }")
    print(f"{c.days = }")
    print(f"{c.seconds = }")
    print(f"{c.seconds / 3600 = }")
    print(f"{c.total_seconds() / 3600 = }")

def recipe2():
    """表示指定的日期和时间"""
    
    a = datetime(2012, 9, 23)
    print(f"{a = }")
    print(f"{a + timedelta(days=10) = }")

    b = datetime(2012, 12, 21)
    d = b - a
    print(f"{d = }")
    print(f"{d.days = }")

    now = datetime.today()
    print(f"{now = }")
    print(f"{now + timedelta(minutes=10) = }")

def recipe3():
    """自动处理闰年"""
    a = datetime(2012, 3, 1)
    b = datetime(2012, 2, 28)
    print(f"{a - b = }")

    c = datetime(2013, 3, 1)
    d = datetime(2013, 2, 28)
    print(f"{c - d = }")

def recipe4():
    """执行更加复杂的日期操作，比如处理时区，模糊时间范围，节假日计算"""
    a = datetime(2012, 9, 23)
    # a + timedelta(years=1) # TypeError
    print(f"{a + relativedelta(months=+1) = }")
    print(f"{a + relativedelta(months=+4) = }")

    b = datetime(2012, 12, 21)
    d = b - a
    print(f"{d = }")

    d = relativedelta(b, a)
    print(f"{d = }")
    print(f"{d.months = }")
    print(f"{d.days = }")

def main():
    print('recipe1'.center(20, '*'))
    recipe1()
    print('recipe2'.center(20, '*'))
    recipe2()
    print('recipe3'.center(20, '*'))
    recipe3()
    print('recipe4'.center(20, '*'))
    recipe4()

if __name__ == '__main__':
    main()      